package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.modular.school.entity.*;
import cn.stylefeng.guns.modular.school.model.params.ClassSchoolTableParam;
import cn.stylefeng.guns.modular.school.model.result.CourseSignResult;
import cn.stylefeng.guns.modular.school.service.*;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ErrorResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.stylefeng.guns.comm.util.DateUtils.distanceSec;


/**
 * 绑定控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/gunsApi/course/sign")
public class ApiNeedTokenCourseSignController extends BaseController {

    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private CourseSignService courseSignService;
    @Autowired
    private ClassSchoolTableService classSchoolTableService;
    @Autowired
    private ClassInfoService classInfoService;

    @RequestMapping(value = "list")
    public Object list(Integer schoolTableId) {
        Map map = new HashMap();
        List<CourseSignResult> list = new ArrayList<>();
        List<CourseSign> courseSigns = courseSignService.list(new QueryWrapper<CourseSign>().eq("type", 1).eq("school_table_id", schoolTableId).eq("sign_date", DateUtil.today()).orderByDesc("create_time"));
        courseSigns.forEach(courseSign -> {
            CourseSignResult courseSignResult = new CourseSignResult();
            ToolUtil.copyProperties(courseSign, courseSignResult);
            StudentInfo student = studentInfoService.getById(courseSign.getStudentTeacherId());
            courseSignResult.setStudentName(student.getName());
            courseSignResult.setStudentSex(student.getSex().equals("M") ? "男" : "女");
            courseSignResult.setCode(student.getCode());
            ClassInfo classInfo = classInfoService.getById(student.getClassId());
            courseSignResult.setClassName(classInfo.getName());
            list.add(courseSignResult);
        });
        map.put("courseSigns", list);

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "leave")
    public Object leave(String userCode, Integer schoolTableId, String signPlace) {
        Map map = new HashMap();
        StudentInfo studentInfo = studentInfoService.findByCode(userCode);
        if (studentInfo == null) {

        } else {
            CourseSign courseSign = new CourseSign();
            courseSign.setSignPlace(signPlace);
            courseSign.setStudentTeacherId(studentInfo.getId());
            courseSign.setType(1);
            courseSign.setSchoolTableId(schoolTableId);
            courseSign.setSignDate(new DateTime());
            courseSign.setSignOrLeave("2");
            courseSignService.save(courseSign);
            // 取课表
            Integer classId = studentInfo.getClassId();
            ClassSchoolTableParam param = new ClassSchoolTableParam();
            param.setClassId(classId);
            param.setStudentInfo(studentInfo);
            map = classSchoolTableService.findStudentSchoolTable(param);

        }
        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "add")
    public Object add(String userCode, Integer schoolTableId, String signPlace) {
        Map map = new HashMap();
        StudentInfo studentInfo = studentInfoService.findByCode(userCode);
        CourseSign courseSign = new CourseSign();
        courseSign.setSignPlace(signPlace);
        courseSign.setSignOrLeave("1");

        // 当日签到check
        ClassSchoolTable schoolTable = classSchoolTableService.getById(schoolTableId);
        String type = schoolTable.getType();
        // 周日的时候变为1
        if (type.equals("7")) {
            type = "1";
        } else {
            type = String.valueOf(Integer.valueOf(type) + 1);

        }

        // 周一
        if (DateUtil.dayOfWeek(new DateTime()) != Integer.valueOf(type)) {
            map.put("activeIndex", 0);
            ErrorResponseData errorResponseData = new ErrorResponseData("只能对当天的课程进行签到");
            return errorResponseData;
        }

        String[] split = schoolTable.getStartEndTime().split("-");
        String startTime = DateUtil.today() + " " + split[0];
        //DateUtil.now();
        if (distanceSec(DateUtil.parse(startTime, DatePattern.NORM_DATETIME_MINUTE_PATTERN), DateUtil.parse(DateUtil.now(), DatePattern.NORM_DATETIME_MINUTE_PATTERN)) > 300) {
            ErrorResponseData errorResponseData = new ErrorResponseData("只能提前5分钟签到");
            return errorResponseData;
        }
//        //周二是3，
//        else if (DateUtil.dayOfWeek(new DateTime()) == 3) {
//            map.put("activeIndex", 1);
//        } else if (DateUtil.dayOfWeek(new DateTime()) == 4) {
//            map.put("activeIndex", 2);
//        } else if (DateUtil.dayOfWeek(new DateTime()) == 5) {
//            map.put("activeIndex", 3);
//        } else if (DateUtil.dayOfWeek(new DateTime()) == 6) {
//            map.put("activeIndex", 4);
//        } else if (DateUtil.dayOfWeek(new DateTime()) == 7) {
//            map.put("activeIndex", 5);
//        } else if (DateUtil.dayOfWeek(new DateTime()) == 1) {
//            map.put("activeIndex", 6);
//        }


        if (studentInfo == null) {
            TeacherInfo teacherInfo = teacherInfoService.getOne(new QueryWrapper<TeacherInfo>().eq("code", userCode));
            courseSign.setStudentTeacherId(teacherInfo.getId());
            courseSign.setType(2);
            courseSign.setSchoolTableId(schoolTableId);
            courseSign.setSignDate(new DateTime());
            courseSignService.save(courseSign);

            ClassSchoolTableParam param = new ClassSchoolTableParam();
            param.setTeacherId(teacherInfo.getId());
            map = classSchoolTableService.findTeacherSchoolTable(param);

        } else {
            courseSign.setStudentTeacherId(studentInfo.getId());
            courseSign.setType(1);
            courseSign.setSchoolTableId(schoolTableId);
            courseSign.setSignDate(new DateTime());
            courseSignService.save(courseSign);

            Integer classId = studentInfo.getClassId();
            // 取课表
            ClassSchoolTableParam param = new ClassSchoolTableParam();
            param.setClassId(classId);
            param.setStudentInfo(studentInfo);
            map = classSchoolTableService.findStudentSchoolTable(param);
        }

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

}


